const {
  ActivityModel
} = require('../../../utils/activity-model.js')
const activityModel = new ActivityModel()
const {
  MemberModel
} = require('../../../utils/member-model.js');
var memberModel = new MemberModel();
var utils = require("../../../utils/util.js");
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    memberId: '',
    assetAvail: 0, //飞豆余额
    shopId: '', //门店id
    authShow: false,
    isDisabled: false,
    gameList: []
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopId: options.shopId
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.getInfo().then(data => {
      if (data.data.login) {
        this.getUserInfo()
        let param = {
          source: 'page'
        }
        let merchantId = wx.getStorageSync('merchantId')
        app.setTrackerLog(merchantId, 3, 4, 1, param)
      } else {
        this.setData({
          authShow: true
        })
      }
    })
  },
  /**
   *微信会员信息
   */
  getUserInfo: function () {
    var that = this;
    memberModel.userInfo({
      merchantId: wx.getStorageSync('merchantId')
    }).then(data => {
      wx.setStorageSync('memberId', data.memberId)
      that.setData({
        memberId: data.memberId || "" //会员ID
      }, function () {
        that.getUserAssetDetail()
        that.getBullGameList()
      });
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },
  getBullGameList () {
    activityModel.getBullGameList({
      memberId: this.data.memberId
    }).then(data => {
      this.setData({
        gameList: data
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },
  /**
   * 会员资产详情
   */
  getUserAssetDetail() {
    var that = this;
    var param = {};
    param.merchantId = wx.getStorageSync('merchantId');
    param.assetType = 1; //飞豆
    param.statistics = true; //是否查询统计数据
    param.assetStatus = 0; //0:正常 3:过期
    param.shopId = that.data.shopId; //门店id
    memberModel.userAssetDetail(param).then(data => {
      that.setData({
        assetAvail: utils.formatCurrency(data.assetAvail) || 0, //飞豆余额
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 捕鱼海岛礼品兑换
   */
  exchange(event) {
    let merchantId = wx.getStorageSync('merchantId')
    let c_giftId = event.currentTarget.dataset.giftid
    let exchanged = event.currentTarget.dataset.exchanged
    let giftNum = event.currentTarget.dataset.giftnum
    if(exchanged){
      wx.showToast({
        title: '兑换次数已达上限',
        icon: 'none',
        duration: 2000
      })
      return
    }
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确认要兑换该礼包吗？',
      success (res) {
        if (res.confirm) {
          if(giftNum<=0){
            wx.showToast({
              title: '此礼包已抢光，请选择其它礼包',
              icon: 'none',
              duration: 2000
            })
            return
          }
          if(that.data.assetAvail < giftNum){
            wx.showToast({
              title: '飞豆不足',
              icon: 'none',
              duration: 2000
            })
            return
          }
          let m_param = {
            code: c_giftId
          }
          app.setTrackerLog(merchantId, 3, 4, 2,m_param)
          that.setData({
            isDisabled: true
          })
          let param = {
            giftId: c_giftId,
            memberId: that.data.memberId,
            shopId: that.data.shopId,
            merchantId: merchantId
          }
          activityModel.exchangeBullGame(param).then(data => {
            wx.showToast({
              title: `兑换成功，兑换码已发放，请到消息中心查看`,
              icon: 'none',
              duration: 2000
            })
            that.setData({
              isDisabled: false
            })
            setTimeout(()=>{
              that.getUserAssetDetail()
              that.getBullGameList()
            }, 1500);
          }).catch(err => {
            that.setData({
              isDisabled: false
            })
            wx.showToast({
              title: `${err.error}`,
              icon: 'none',
              duration: 2000
            })
          })
        } else if (res.cancel) {
        }
      }
    })
    
  },

  // 授权成功的回调
  loginSucceed: function () {
    this.getUserInfo()
  },
  goGameUrl(){
    wx.setClipboardData({
      data:'https://download.buff.vip/12403',
      success(res) {
        wx.getClipboardData({
          success(res) {
            wx.showToast({
              title: `复制成功，请使用浏览器访问下载游戏`,
              icon: 'none',
              duration: 2000
            })
          }
        })
      }
    })
  }
})